﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuDada.Core.Post;
using WuDada.Core.Post.Service;
using WuDada.Core.Post.Domain;
using Common.Logging;

public partial class map_in : System.Web.UI.Page
{
    private ILog m_Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    private PostFactory m_PostFactory;
    private IPostService m_PostService;
    private SessionHelper m_SessionHelper;

    protected void Page_Load(object sender, EventArgs e)
    {
        m_PostFactory = new PostFactory();
        m_PostService = m_PostFactory.GetPostService();
        m_SessionHelper = new SessionHelper();

        if (m_SessionHelper.Member == null)
        {
            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJSAndRedirect("請先登入會員。", "login.aspx"), false);
            return;
        }

        if (!IsPostBack)
        {
            hdnMemberId.Value = m_SessionHelper.Member.MemberId.ToString();
            lblMemberName.Text = m_SessionHelper.Member.Name;
            FillList();
        }
    }

    private void FillList()
    {
        Dictionary<string, string> conditions = new Dictionary<string, string>();
        conditions.Add("MemberId", hdnMemberId.Value);
        conditions.Add("Flag", "1");
        conditions.Add("Order", "order by p.CreatedDate desc ");

        //時數總合
        conditions.Add("SumFieldName", "p.CustomField2");
        ltlTotalLearnHours.Text = m_PostService.GetSumPostField(conditions).ToString();

        //分頁
        AspNetPager1.RecordCount = m_PostService.GetPostCount(conditions);        
        int pageIndex = (AspNetPager1.CurrentPageIndex - 1);
        int pageSize = AspNetPager1.PageSize;
        conditions.Add("PageIndex", pageIndex.ToString());
        conditions.Add("PageSize", pageSize.ToString());

        RepeaterList.DataSource = m_PostService.GetPostList(conditions);
        RepeaterList.DataBind();
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        FillList();
    }

    protected void RepeaterList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        //if (e.Item.ItemIndex != -1)
        //{
        //    Control ctrl = e.Item;
        //    PostVO postVO = (PostVO)e.Item.DataItem;
        //}
    }
}